<?php
class login
{
	var $popupStyle;
	var $warning;
	function __construct()
	{
		$this->popupStyle="none";
		if(isset($_POST['auth']))
		{
			$login=strtolower($this->safetyCheck($_POST['login']));
			$password=$this->safetyCheck($_POST['password']);
			$password=md5($password);
			$aConnect=new connect('main');
			$res=$aConnect->select_all('users', "WHERE login='$login' AND password='$password'");
			if(mysql_num_rows($res))
				{
					$_SESSION['login']=$login;
					$row=mysql_fetch_array($res);
					$_SESSION['userStatus']=$row['status'];
header('location:pcabinet.php?city='.$_GET['city']);
				}
			else
				{
					$this->warning="Неверный логин или пароль";
		    		$this->popupStyle="block";
				}
		}
	}
	function safetyCheck($value)
	{
		$value=@ strip_tags($value);
		$value=htmlspecialchars($value);
		$value=addslashes($value);
		return $value;
	}
	function showLoginLink()
	{
		echo ('
		<link rel="stylesheet" type="text/css" href="style/auth.css" />
		');
		if(!isset($_SESSION['login']))
		{
			/*echo ('
			<div id="enterLink">
				<a href="#"  onclick="document.getElementById(\'parent_popup\').style.display=\'block\';">Войти</a>
			</div>

			');*/
			echo ('
			<input id="enterLink" type="submit" value="Войти" onclick="document.getElementById(\'parent_popup\').style.display=\'block\';">
			<div id="registrLink">
				<a href="registr.php?city='.$_GET['city'].'">Регистрация</a>
			</div>
			');
		}
		else
		{
			$newMessage;
			$aConnect=new connect('main');
			/*if($countMess=mysql_num_rows($aConnect->select_all('messages', "WHERE user='".$_SESSION['login']."' AND status='0'")))
				$newMessage='<img src="images/hyu.jpg" />'; */
			echo ('
				<form name="logout" action="actions/logout.php?city='.$_GET['city'].'" method="post">
			    	<login>'.$_SESSION['login'].'
			    	<div id="registrLink">
						<a href="pcabinet.php?city='.$_GET['city'].'">'.$newMessage.'Личный кабинет</a>
					</div>
			    	</login><input id="enterLink" type="submit" value="Выйти" name="logout">
				</form>
			');

		}
		echo ('
		<div id="parent_popup" style="display: '.$this->popupStyle.';">
			<div id="popup" align="center">
				<div id="warning">
					'.$this->warning.'
				</div>
				<form name="auth" action="" method="post">
					Введте логин:<br />
		        	<input name="login" type="text" value=""><br />
		        	Введте Пароль:<br />
		        	<input name="password" type="password" value=""><br />
		        	<a id="forgot" href="forget.php">Забыли пароль?</a><br />
		        	<input type="submit" name="auth" value="Войти"><br />
		        	<a id="registr" href="registr.php">Зарегистрироваться</a><br />
				</form>
				<a id="close" title="Закрыть" onclick="document.getElementById(\'parent_popup\').style.display=\'none\';"></a>
			</div>
		</div>
		');
	}
}